<template>
  <a-card>
    <div :class="advanced ? 'search' : null">
      <a-form layout="horizontal" ref="form" :form="formData">
        <div :class="advanced ? null: 'fold'">
          <a-row>
            <a-col :md="8" :sm="24">
              <a-form-item
                  label="名字"
                  :labelCol="{span: 5}"
                  :wrapperCol="{span: 18, offset: 1}"
              >
                <a-input v-model="formData.name" placeholder="请输入"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item
                  label="性别"
                  :labelCol="{span: 5}"
                  :wrapperCol="{span: 18, offset: 1}"
              >
                <a-select v-model="formData.sex" placeholder="请选择">
                  <a-select-option
                      v-for="item in sexOptions"
                      :key="item.id"
                      :value="item.value"
                      :label="item.name"
                  >
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </a-row>
        </div>
        <span style="float: right; margin-top: 3px;">
          <a-button type="primary" @click="search">查询</a-button>
          <a-button style="margin-left: 8px" @click="reset">重置</a-button>
          <a @click="toggleAdvanced" style="margin-left: 8px">
            {{ advanced ? '收起' : '展开' }}
            <a-icon :type="advanced ? 'up' : 'down'"/>
          </a>
        </span>
      </a-form>
    </div>
    <div>
      <a-space class="operator">
        <a-button v-auth="`user-add`" @click="addRecord" type="primary">新建</a-button>
        <a-button v-auth="`user-export`" @click="exportExcel">导出</a-button>
      </a-space>
      <standard-table
          :columns="columns"
          :dataSource="dataSource"
          :selectedRows.sync="selectedRows"
          @clear="onClear"
          @change="onChange"
          :pagination="{...pagination, onChange: onPageChange}"
          @selectedRowChange="onSelectChange"
      >
        <template slot="roleNamesIndex" slot-scope="{ text }">
          <a-tag v-for="(role, index) in text" :color="sysConfig.theme.color" :key="index">
            {{ role }}
          </a-tag>
        </template>
        <template slot="sexIndex" slot-scope="{ text }">
          {{ text === '1' ? '男' : '女' }}
        </template>
        <div slot="action" slot-scope="{text, record}">
          <a @click="editRecord(record)" v-auth:permission="`user-edit`" style="margin-right: 8px">
            <a-icon type="edit"/>
            编辑
          </a>
          <!-- v-auth="`*`"-->
          <a @click="deleteRecord(record.id)" v-auth:permission="`user-delete`" style="margin-right: 8px">
            <a-icon type="delete"/>
            删除
          </a>
          <a @click="viewRecord(record)" v-auth:permission="`user-view`" style="margin-right: 8px">
            <a-icon type="search"/>
            详情
          </a>
        </div>
        <template slot="statusTitle">
          <a-icon @click.native="onStatusTitleClick" type="info-circle"/>
        </template>
      </standard-table>
      <user-form ref="userModal" @refresh="reset"/>
    </div>
  </a-card>
</template>

<script>
import StandardTable from '@/components/table/StandardTable'
import UserForm from '@/pages/user/form'
import {columns, page, del} from '@/services/user';
import sysConfig from '@/config/config'
import {single} from '@/services/dict'

export default {
  name: 'UserList',
  components: {StandardTable, UserForm},
  data() {
    return {
      sysConfig,
      formData: {},
      sexOptions: [],
      advanced: false,
      columns: columns,
      dataSource: [],
      selectedRows: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  // authorize: {
  //   deleteRecord: '*'
  // },
  mounted() {
    this.initDict()
    this.getData()
  },
  methods: {
    onPageChange(page, pageSize) {
      this.pagination.current = page
      this.pagination.pageSize = pageSize
      this.getData()
    },
    getData() {
      page({
        page: this.pagination.current,
        pageSize: this.pagination.pageSize,
        ...this.formData
      }).then(res => {
        const {listData, page, pageSize, total} = res?.data?.data ?? {}
        this.dataSource = listData
        this.pagination.current = page
        this.pagination.pageSize = pageSize
        this.pagination.total = total
      })
    },
    addRecord() {
      this.$nextTick(() => {
        this.$refs.userModal.add()
      })
    },
    editRecord(record) {
      this.$nextTick(() => {
        this.$refs.userModal.edit(record)
      })
    },
    deleteRecord(id) {
      this.$confirm({
        title: '数据删除',
        content: '确定删除吗?',
        okText: '确定',
        cancelText: '取消',
        onOk: () => {
          del(id).then(res => {
            if (res.data.code === 200) {
              this.$message.success('删除成功!' || res.data.msg);
              this.search();
            } else {
              this.$message.error('删除失败!' || res.data.msg);
            }
          })
        },
        onCancel: () => {
          console.log('用户点击了取消');
        }
      });
    },
    viewRecord(record) {
      this.$nextTick(() => {
        this.$refs.userModal.view(record)
      })
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    remove() {
      this.selectedRows = []
    },
    onClear() {
      console.log('$f=>onClear=>清空了勾选的所有行')
    },
    onStatusTitleClick() {
      console.log('$f=>onStatusTitleClick=>点击了状态栏表头')
    },
    onChange() {
      console.log('$f=>onChange=>表格状态改变了')
    },
    onSelectChange() {
      console.log('$f=>onSelectChange=>选中行改变了')
    },
    exportExcel() {
      console.log('$f=>export=>留作导出')
    },
    importExcel() {
      console.log('$f=>export=>留作导入')
    },
    search() {
      this.pagination.current = 1
      this.getData()
    },
    reset() {
      this.pagination.current = 1
      this.formData = {}
      this.getData()
    },
    initDict() {
      single('sex').then(res => {
        if (res.data.code === 200) {
          this.sexOptions = res.data.data
        } else {
          this.roleOptions = []
        }
      })
    }
  }
}
</script>

<style lang="less">
.search {
  margin-bottom: 54px;
}

.fold {
  width: calc(100% - 216px);
  display: inline-block
}

.operator {
  margin-bottom: 18px;
}

@media screen and (max-width: 900px) {
  .fold {
    width: 100%;
  }
}
</style>
